<?php
/**
 * Created by PhpStorm.
 * User: 董坤鸿
 * Date: 2017/1/13
 * Time: 上午11:21
 */

namespace EWeiLink\JuPai;

use Jenssegers\Mongodb\Eloquent\Model;


/**
 * Class Message
 * @package EWeiLink\JuPai
 *
 * @property string $id
 * @property string $msgId
 * @property string $clientUin
 * @property string $content
 * @property string $type
 * @property string $to
 * @property string $from
 * @property int $createTime
 */
class Message extends Model
{
    protected $connection = Jupai::plugin_database_name;
    protected $guarded = [];

    /**
     * 获取消息数组
     * @return array|null
     */
    public function getMessageArray()
    {
        $contacts = Contact::find($this->to);
        if ($contacts && $contacts->wechatInfo && $contacts->wechatInfo->account) {
            return [
                'clientUin' => $contacts->clientUin,     //clientUin
                'content' => $this->content,   //消息内容
                'type' => $this->type, //消息类型
                'to' => $contacts->wechatInfo->account,  //联系人
            ];
        }
        return null;
    }

    /*
     *  根据聊天内容搜索
     */
    public function scopeSearch($query, $inputs){
        if(!empty($inputs)){
            return $query->where(function ($querys) use ($inputs) {
                if (isset($inputs['content']) && trim(strlen($inputs['content'])) > 0) {
                    $querys->where('type', 'text_msg')
                        ->where('content', 'like', "%{$inputs['content']}%");
                }
            });
        }
        return $query;
    }
    
}
